﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Runtime;
using System.ComponentModel;
using System.Collections;

namespace OpenSource.Controls
{
    public class InputFormRowCollection : StateManagedCollection
    {
        private static readonly Type[] knownTypes = new Type[2]
                                                        {
                                                            typeof (InformationRow),
                                                            //typeof (SelectRow),
                                                            //typeof (CommandRow),
                                                            typeof (TemplateRow)
                                                        };

        private EventHandler FieldsChanged;

        [Browsable(false)]
        public InputFormRow this[int index]
        {
            get { return ((IList) this)[index] as InputFormRow; }
        }

        //public event EventHandler FieldsChanged
        //{
        //    add
        //    {
        //        EventHandler eventHandler = this.FieldsChanged;
        //        EventHandler comparand;
        //        do
        //        {
        //            comparand = eventHandler;
        //            eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.FieldsChanged, comparand + value, comparand);
        //        }
        //        while (eventHandler != comparand);
        //    }
        //    remove
        //    {
        //        EventHandler eventHandler = this.FieldsChanged;
        //        EventHandler comparand;
        //        do
        //        {
        //            comparand = eventHandler;
        //            eventHandler = Interlocked.CompareExchange<EventHandler>(ref this.FieldsChanged, comparand - value, comparand);
        //        }
        //        while (eventHandler != comparand);
        //    }
        //}

        static InputFormRowCollection()
        {
        }

        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        public InputFormRowCollection()
        {
        }

        public void Add(InputFormRow field)
        {
            ((IList)this).Add(field);
        }

        public InputFormRowCollection CloneFields()
        {
            InputFormRowCollection controlFieldCollection = new InputFormRowCollection();
            foreach (InputFormRow InputFormRow in (StateManagedCollection)this)
                controlFieldCollection.Add(InputFormRow.CloneField());
            return controlFieldCollection;
        }

        public bool Contains(InputFormRow field)
        {
            return ((IList)this).Contains(field);
        }

        public void CopyTo(InputFormRow[] array, int index)
        {
            ((IList)this).CopyTo(array, index);
            return;
        }

        protected override object CreateKnownType(int index)
        {
            switch (index)
            {
                case 0:
                    return (object)new InformationRow();
                //case 1:
                //    return (object)new SelectRow();
                //case 2:
                //    return (object)new CommandRow();
                case 3:
                    return (object)new TemplateRow();    
                default:
                    throw new ArgumentOutOfRangeException(CustomString.GetString("InputFormRowCollection_InvalidTypeIndex"));
            }
        }

        protected override Type[] GetKnownTypes()
        {
            return InputFormRowCollection.knownTypes;
        }

        public int IndexOf(InputFormRow field)
        {
            return ((IList)this).IndexOf(field);
        }

        public void Insert(int index, InputFormRow field)
        {
            ((IList)this).Insert(index, field);
        }

        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        protected override void OnClearComplete()
        {
            this.OnFieldsChanged();
        }

        private void OnFieldsChanged()
        {
            if (this.FieldsChanged == null)
                return;
            this.FieldsChanged((object)this, EventArgs.Empty);
        }

        protected override void OnInsertComplete(int index, object value)
        {
            InputFormRow InputFormRow = value as InputFormRow;
            if (InputFormRow != null)
                InputFormRow.FieldChanged += new EventHandler(this.OnFieldChanged);
            this.OnFieldsChanged();
        }

        protected override void OnRemoveComplete(int index, object value)
        {
            InputFormRow InputFormRow = value as InputFormRow;
            if (InputFormRow != null)
                InputFormRow.FieldChanged -= new EventHandler(this.OnFieldChanged);
            this.OnFieldsChanged();
        }

        protected override void OnValidate(object o)
        {
            base.OnValidate(o);
            if (!(o is InputFormRow))
                throw new ArgumentException(CustomString.GetString("InputFormRowCollection_InvalidType"));
        }

        public void RemoveAt(int index)
        {
            this.RemoveAt(index);
        }

        public void Remove(InputFormRow field)
        {

            ((IList)this).Remove(field);
        }

        protected override void SetDirtyObject(object o)
        {
            ((InputFormRow)o).SetDirty();
        }

        private void OnFieldChanged(object sender, EventArgs e)
        {
            this.OnFieldsChanged();
        }



        internal object SaveViewState()
        {
            IStateManager manager = (IStateManager) this;
            return manager.SaveViewState();
        }



        internal void TrackViewState()
        {
            IStateManager manager = (IStateManager)this;
            manager.TrackViewState();
        }

    }
}